package com.h4313.ghome.ui.fragments;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.h4313.ghome.GHomeApplication;
import com.h4313.ghome.R;
import com.h4313.ghome.io.TemperatureTask;
import com.h4313.ghome.io.TemperatureTask.TemperatureCallback;
import com.h4313.ghome.io.TemperaturesTask;
import com.h4313.ghome.io.TemperaturesTask.TemperaturesCallback;
import com.h4313.ghome.model.Temperature;
import com.h4313.ghome.model.TemperatureAllDay;
import com.h4313.ghome.utils.UIUtils;

public class TemperatureFragment extends Fragment implements TemperatureCallback, TemperaturesCallback, Refreshable {
	
	protected TextView temperature;
	protected ProgressBar barre;
	protected WebView webView;
	protected TextView tvMaj;
	
	private static final String FORMAT_TEMPERATURE = "%.2f °C";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View v = inflater.inflate(R.layout.fragment_temperature, container, false);
		temperature = (TextView) v.findViewById(R.id.tv_temperature);
		barre = (ProgressBar) v.findViewById(R.id.progressBar_temperature);
		webView = (WebView) v.findViewById(R.id.wv_chart);
		tvMaj = (TextView) v.findViewById(R.id.tv_maj);
		
		webView.loadUrl( "http://chart.apis.google.com/" +
				"chart?cht=lc&chs=300x200&" +
				"chtt=Dernieres 24h&" +
				"chg=0,12.5,5,5&" +
				"chxr=1,0,40&" +
				"chxt=x,y&" +
				"chxl=1:|0|10|20|30|40&" +
				"chds=0,40&" +
				"chd=t:5,10,20");
//		webView.loadUrl("http://chart.apis.google.com/chart?" +
//		"cht=lc&chs=450x330&chd=t:7,18,11,26,22,11,14&"+
//		"chxr=1,0,30&chds=0,30&"+
//		"chco=4d89f9&"+
//		"chxt=x,y&chxl=0:|Mon|Tue|Wed|Thu|Fri|Sat|Sun&"+
//		"chls=3,1,0&"+
//		"chm=d,4d89f9,0,0,12,0|d,4d89f9,0,1,12,0|d,4d89f9,0,2,12,0|d,4d89f9,0,3,12,0|d,4d89f9,0,4,12,0|d,4d89f9,0,5,12,0|d,4d89f9,0,6,12,0&" +
//		"chg=0,6.67,5,5");

		return v;
	}

	@Override
	public void onTemperatureFound(Temperature temp) {
		
		if (temp == null) {
			UIUtils.showToast(this, "Impossible de charger la température", Toast.LENGTH_SHORT);
			return;
		}
		
		// TODO afficher la temperature
		Date dateInt = temp.getDate();
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTime(dateInt);
		
		String dateString = Integer.toString(dateCal.get(Calendar.DAY_OF_MONTH)) + '/' 
				+ Integer.toString(dateCal.get(Calendar.MONTH)) + '/'
				+ Integer.toString(dateCal.get(Calendar.YEAR));
		String heureString = Integer.toString(dateCal.get(Calendar.HOUR_OF_DAY)) + "h"
				+ dateCal.get(Calendar.MINUTE);
		
		
		tvMaj.setText("Derniere mise à jour le : " + dateString + " à " + heureString);
		
		temperature.setText(String.format(FORMAT_TEMPERATURE, temp.getTemperature()));
		temperature.setVisibility(View.VISIBLE);
	
		Animation disparait = AnimationUtils.loadAnimation(getActivity(), R.anim.disparait);
		Animation apparait = AnimationUtils.loadAnimation(getActivity(), R.anim.apparait);
		
		barre.startAnimation(disparait);
		temperature.startAnimation(apparait);
		
		barre.setVisibility(View.GONE);

		
		//if (getActivity() != null) {
		//	Toast.makeText(getActivity(), temp.getTemperature(), Toast.LENGTH_SHORT).show();
		//}
	}
	
	@Override
	public void onTemperaturesFound(TemperatureAllDay tempAllDay){
		
		Log.d("Margot", "onTemperaturesFound");
		String coords = "";
		if(tempAllDay == null){
			UIUtils.showToast(this, "Impossible de charger les 24 dernieres temperatures", Toast.LENGTH_SHORT);
			return;
		}
		for(int i = 0 ; i<tempAllDay.getTemperatures().size(); i++){
			Double tempDouble = tempAllDay.getTemperatures().get(i);
			coords += Double.toString(tempDouble);
			if(i != tempAllDay.getTemperatures().size()-1 ){
				coords += ",";
			}
		}
		String chart = "http://chart.apis.google.com/" +
				"chart?cht=lc&chs=300x150&chtt=Dernieres 24h&chg=0,33.33,5,5&chds=0,40&chd=t:" +
				coords;
		
		
		webView.loadUrl(chart);
	}

	@Override
	public void refresh() {
		
		barre.setVisibility(View.VISIBLE);

		String stringLastOneUrl = GHomeApplication.SERVER_URL +  "/temperature/lastOne";
		String stringAllDayUrl = GHomeApplication.SERVER_URL + "/temperature/last24h"; //pas sure

		Activity activity = this.getActivity();
		ConnectivityManager connMgr = (ConnectivityManager)
				activity.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		
		if(networkInfo != null && networkInfo.isConnected()){
			
			
			
			TemperatureTask task = new TemperatureTask(this);
			task.execute(stringLastOneUrl);
			
			TemperaturesTask taskAllDay = new TemperaturesTask(this);
			taskAllDay.execute(stringAllDayUrl);
			Log.d("Margot", "ok");
		}
		else{
			System.out.print("No Network available");
		}
	}
	
}
